/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.common.utility;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * description:
 *
 * @author Noah Guo
 * @date 2020/06/16
 */
public class Base64Utility {
    private Base64Utility() {
    }

    /**
     * 解码base64 编码后的字符串
     *
     * @param content
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String decode(String content) throws UnsupportedEncodingException {
        final Base64.Decoder decoder = Base64.getDecoder();
        return new String(decoder.decode(content), StandardCharsets.UTF_8);
    }


    /**
     * 编码字符串
     *
     * @param content
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode(String content) throws UnsupportedEncodingException {
        if (StringUtility.isNullOrEmpty(content)) {
            content = "";
        }

        final Base64.Encoder encoder = Base64.getEncoder();
        final byte[] textByte = content.getBytes(StandardCharsets.UTF_8);
//编码
        return encoder.encodeToString(textByte);
    }

}
